package view;

import inventory.InventoryItem;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.Model;
import model.UserPage;

/**
 * UserPageLibraryXML implements the Library/Book Inventoru XML display of a
 * user's signed-in dashboard, populating the display the listings and
 * information of all their registered books.
 *
 * Primarily used for the Android side of the Lithub project.
 * 
 * @author Reynaldo Gonzales.
 * @version 1.0
 *
 */
class UserPageLibraryXML extends View {

    private Model model;
    private HttpServletRequest request;
    private HttpServletResponse response;

    /**
     * UserPageLibraryXML(...) is a reciever of user dashboard page view
     * requests for the web based Library/Book Inventory Div in XML and also the
     * responder to such as well.
     *
     * @param a HttpServletRequest request which holds the HTTP request
     * information of the requesting user.
     *
     * @param b HttpServletResponse response to the requesting user.
     *
     */
    UserPageLibraryXML(HttpServletRequest req, HttpServletResponse res) {
        request = req;
        response = res;
    }

    /**
     * invoke(modem m) is a model invoker which casts the invokation of the
     * UserPage/Library web based page model implementation in XML to try to provide
     * a user with a display of the the book listings and information of all
     * their registered books.
     *
     * @param a model which refers to the model that implements the
     * UserPageDiscoverDiv.
     *
     * @catch generic exception.
     *
     */
    @Override
    public void invoke(Model m) {
        PrintWriter out = null;
        try {
            UserPage u = (UserPage) m;

            out = response.getWriter();
            response.setContentType("text/xml");
            out.print("<body copyright='All data copyright LitHub 2012'>");

            for (InventoryItem key : u.library.keySet()) {
                out.print("<book>");

                out.print("<isbn>" + key.itemISBN + "</isbn>");
                out.print("<title>" + key.itemName + "</title>");
                out.print("<author>" + key.itemAuthor + "</author>");
                out.print("<status>" + key.status + "</status>");

                out.print("</book>");
            }
            out.print("</body>");
        } catch (Exception ex) {
            Logger.getLogger(FrontPageHTML.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
